package io.summer.gadget.channel.core.autoconfig;

import io.summer.gadget.channel.SummerChannel;
import io.summer.gadget.channel.core.MessageDispatcher;
import io.summer.gadget.channel.core.Protocol;
import io.summer.gadget.channel.core.ServerChooser;
import io.summer.gadget.channel.core.ServiceCheck;
import io.summer.gadget.channel.core.ServiceRegistry;
import io.summer.gadget.channel.core.TopicUtil;
import io.summer.gadget.channel.core.impl.DefaultServerChooser;
import io.summer.gadget.channel.core.impl.JacksonProtocol;
import io.summer.gadget.channel.core.impl.MessageDispatcherImpl;
import io.summer.gadget.channel.core.impl.SummerChannelImpl;
import io.summer.redis.config.RedisToolConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@ConditionalOnProperty(prefix = "summer.gadget.channel", value = "enable", havingValue = "true")
@Import({RedisToolConfig.class})
public class ChannelAutoConfig {
  @Bean
  @ConditionalOnMissingBean(Protocol.class)
  public Protocol protocol() {
    return new JacksonProtocol();
  }

  @Bean
  @ConditionalOnMissingBean(ServerChooser.class)
  public ServerChooser serverChooser() {
    return new DefaultServerChooser();
  }

  @Bean
  public TopicListenerRegistry topicListenerRegistry() {
    return new TopicListenerRegistry();
  }

  @Bean
  public ServiceCheck serviceCheck() {
    return new ServiceCheck();
  }

  @Bean
  public ServiceRegistry serviceRegistry(ServiceCheck serviceCheck) {
    return new ServiceRegistry(serviceCheck);
  }

  @Bean
  public SummerChannel summerChannel() {
    return new SummerChannelImpl();
  }

  @Bean
  public TopicUtil topicUtil() {
    return new TopicUtil();
  }

  @Bean
  public MessageDispatcher messageDispatcher(SummerChannel summerChannel, Protocol protocol) {
    return new MessageDispatcherImpl(summerChannel, protocol);
  }

  @Bean
  public ChannelProxyRegistry channelProxyRegistry(SummerChannel summerChannel) {
    return new ChannelProxyRegistry(summerChannel);
  }
}
